﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Model.ModelsExt;
using AntUnion.FrameWork.Web.IService;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AntUnion.FrameWork.Web.Service
{
    public class Mt5Service : IMt5Service
    {
        /// <summary>
        ///  获取所有CRM账户
        /// </summary>
        /// <param name="crmId"></param>
        /// <returns></returns>
        public List<Mt5User> GetMt5Users(int crmId)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<Mt5User> mt5UserList = mtContext.Mt5Users.Where(k => k.CrmId == crmId).OrderBy(k => k.Type).ToList();
                    return mt5UserList;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取对应类型的CRM账户
        /// </summary>
        /// <param name="crmId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<Mt5UserModel> GetMt5User(int crmId, int type)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<Mt5UserModel> mt5UserModels = new List<Mt5UserModel>();
                    List<Mt5User> mt5Users = mtContext.Mt5Users.Where(k => k.CrmId == crmId && k.Type == type).ToList();
                    if (mt5Users != null)
                    {
                        foreach (Mt5User mt5User in mt5Users)
                        {
                            Mt5UserModel mt5UserModel = new Mt5UserModel();
                            mt5UserModel.Login = mt5User.Login;
                            mt5UserModel.Type = mt5User.Type;
                            mt5UserModel.CrmId = mt5User.CrmId;
                            mt5UserModel.Balance = mt5User.Balance;
                            mt5UserModel.Currency = Currency.USD.ToString();
                            mt5UserModels.Add(mt5UserModel);
                        }

                    }
                    return mt5UserModels;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        ///  获取对应ID的MT5账户
        /// </summary>
        /// <param name="crmId"></param>
        /// <returns></returns>
        public Mt5User GetMt5UserById(int login, int crmId, int type)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    Mt5User mt5User = mtContext.Mt5Users.Where(k => k.Login == login && k.CrmId == crmId && k.Type == type).FirstOrDefault();
                    return mt5User;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        ///  获取对应ID的MT5账户
        /// </summary>
        /// <param name="crmId"></param>
        /// <returns></returns>
        public Mt5User GetMt5UserById(int crmId)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    Mt5User mt5User = mtContext.Mt5Users.Where(k => k.CrmId == crmId && k.Type == 0).FirstOrDefault();
                    return mt5User;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        ///  更新账户余额
        /// </summary>
        /// <param name="crmId"></param>
        /// <returns></returns>
        public int UpdateMt5Balance(long login, double balance)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    Mt5User mt5User = mtContext.Mt5Users.FirstOrDefault(k => k.Login == login && k.Type == 0);
                    if (mt5User != null)
                    {
                        mt5User.Balance = Convert.ToDecimal(balance);
                        mt5User.UpdateDate = DateTime.Now;
                        return mtContext.SaveChanges();
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return 0;
        }
    }
}
